package com.randy.recyclerviews.adapter;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import java.util.List;

/**
 * @author xudong
 * @since 2017/2/7
 */

public class CollectionsHelper {
    @Nullable
    public static <T> T get(@Nullable List<T> list, int index, @Nullable T defaultValue) {
        return (list != null && index >= 0 && index < list.size()) ? list.get(index) : defaultValue;
    }

    @Nullable
    public static <T> T getOrNull(@Nullable List<T> list, int index) {
        return get(list, index, null);
    }

    @Nullable
    public static <T> T get(@Nullable T[] list, int index, @Nullable T defaultValue) {
        return (list != null && index >= 0 && index < list.length) ? list[index] : defaultValue;
    }

    @Nullable
    public static <T> T getOrNull(@Nullable T[] list, int index) {
        return get(list, index, null);
    }

    @Nullable
    public static <T> T first(@Nullable List<T> list, @Nullable T defaultValue) {
        if (list == null || list.isEmpty()) return defaultValue;
        return list.get(0);
    }

    @Nullable
    public static <T> T first(@Nullable List<T> list) {
        return first(list, null);
    }

    @NonNull
    public static <T> T firstNonNull(@Nullable List<T> list, @NonNull T defaultValue) {
        T first = first(list, null);
        return first == null ? defaultValue : first;
    }

    @Nullable
    public static <T> T first(@Nullable T[] list, @Nullable T defaultValue) {
        if (list == null || list.length == 0) return defaultValue;
        return list[0];
    }

    @Nullable
    public static <T> T first(@Nullable T[] list) {
        return first(list, null);
    }

    @NonNull
    public static <T> T firstNonNull(@Nullable T[] list, @NonNull T defaultValue) {
        T first = first(list, null);
        return first == null ? defaultValue : first;
    }

    @Nullable
    public static <T> T last(@Nullable List<T> list, @Nullable T defaultValue) {
        if (list == null || list.isEmpty()) return defaultValue;
        int count = list.size();
        return list.get(count - 1);
    }

    @Nullable
    public static <T> T last(@Nullable List<T> list) {
        return last(list, null);
    }

    @NonNull
    public static <T> T lastNonNull(@Nullable List<T> list, @NonNull T defaultValue) {
        T last = last(list, null);
        return last == null ? defaultValue : last;
    }

    @Nullable
    public static <T> T last(@Nullable T[] list, @Nullable T defaultValue) {
        if (list == null || list.length == 0) return defaultValue;
        int count = list.length;
        return list[count - 1];
    }

    @Nullable
    public static <T> T last(@Nullable T[] list) {
        return last(list, null);
    }

    @NonNull
    public static <T> T lastNonNull(@Nullable T[] list, @NonNull T defaultValue) {
        T last = last(list, null);
        return last == null ? defaultValue : last;
    }

    public static <T> boolean listEquals(List<T> list, List<T> anotherList) {
        if (list == anotherList) {
            return true;
        }
        int length;
        if (list != null && anotherList != null && (length = list.size()) == anotherList.size()) {
            for (int i = 0; i < length; i++) {
                T object1 = list.get(i);
                T object2 = anotherList.get(i);
                if (!(object1 == null ? object2 == null : object1.equals(object2))) {
                    return false;
                }
            }
            return true;
        }
        return false;
    }
}
